import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BookList {

    List<Book> books = new ArrayList<>();

    int usedSize;

    public BookList() {
        books.add(new Book("三国演义", 45, "罗贯中", 9854));
        books.add(new Book("西游记", 40, "吴承恩", 8723));
        books.add(new Book("红楼梦", 50, "曹雪芹", 3548));
        books.add(new Book("水浒传", 60, "施耐庵", 10854));
        books.add(new Book("软件工程导论", 65, "王安生", 12564));
        books.add(new Book("高等数学", 45, "同济大学数学系", 15684));
        usedSize = 6;
        sort();
    }

    public void add() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要添加书的名字");
        String name = scanner.nextLine();
        System.out.println("请输入你要添加书的作者");
        String author = scanner.nextLine();
        System.out.println("请输入你要添加书的价格");
        int price = scanner.nextInt();
        System.out.println("请输入你要添加书的销量");
        int quantity = scanner.nextInt();


        Book book = new Book(name, price, author, quantity);
        books.add(book);
        usedSize++;
        System.out.println("添加成功");
        sort();
    }

    private void sort() {
        for (int i = 0; i < usedSize - 1; i++) {
            for (int j = 0; j < usedSize - 1 - i; j++) {
                if (!books.get(j).equals(books.get(j + 1))) {
                    Book temp = books.get(j);
                    books.set(j, books.get(j + 1));
                    books.set(j + 1, temp);
                }
            }
        }
    }


    public void show() {
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 5; i++) {
            System.out.println(books.get(i).toString());
        }
        if (usedSize < 5) {
            System.out.println("没有下一页");
            return;
        }
        System.out.println("1-》下一页 0-》退出");

        while (true) {
            int ch = scanner.nextInt();
            if (ch == 1) {
                int k = (Math.min(usedSize, 10));
                for (int i = 5; i < k; i++) {
                    System.out.println(books.get(i).toString());
                }
                System.out.println("2-》上一页 0-》退出");
                continue;
            }
            if (ch == 2) {
                for (int i = 0; i < 5; i++) {
                    System.out.println(books.get(i).toString());
                }
                System.out.println("1-》下一页 0-》退出");
                continue;
            }
            if (ch == 0){
                break;
            }
        }
    }

    public void showAll(){
        for (int i = 0; i < usedSize; i++) {
            System.out.println(books.get(i).toString());
        }
    }
}
